package com.bayss.core.service;

import com.bayss.core.entity.rbac.OBGPermissionsInfo;
import com.bayss.core.entity.rbac.OBGPermissionsInfoExample;
import com.bayss.core.entity.rbac.mapper.OBGPermissionsInfoMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2020/1/5.
 */
@Service
public class PermissionsService {
    private static Logger logger = LogManager.getLogger();
    @Autowired
    private OBGPermissionsInfoMapper permissionsInfoMapper;

    public void save(OBGPermissionsInfo obgPermissionsInfo){
        permissionsInfoMapper.insert(obgPermissionsInfo);
    }

    public OBGPermissionsInfo findById(Integer id) {
        return permissionsInfoMapper.selectByPrimaryKey(id);
    }

    public List<OBGPermissionsInfo> findAll() {
        return permissionsInfoMapper.selectByExample(new OBGPermissionsInfoExample());
    }

    public void deleteByIds(List<Integer> ids){
        //删除权限
        OBGPermissionsInfoExample example = new OBGPermissionsInfoExample();
        OBGPermissionsInfoExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        permissionsInfoMapper.deleteByExample(example);
    }

    public List<OBGPermissionsInfo> findChildrenById(Integer id) {
        OBGPermissionsInfoExample example = new OBGPermissionsInfoExample();
        OBGPermissionsInfoExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<OBGPermissionsInfo> permissionsInfos = permissionsInfoMapper.selectByExample(example);
        return permissionsInfos;
    }

    public void updateOBGPermissionsInfo(OBGPermissionsInfo obgPermissionsInfo) {
        obgPermissionsInfo.setCreateTime(new Date());
        obgPermissionsInfo.setStatus(Byte.valueOf("1"));
        permissionsInfoMapper.updateByPrimaryKeySelective(obgPermissionsInfo);
    }

    /**
     * find by url
     * @param url
     * @return
     */
    public OBGPermissionsInfo findPermissionsByUrl(String url) {
        OBGPermissionsInfoExample example = new OBGPermissionsInfoExample();
        OBGPermissionsInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUrlEqualTo(url);
        List<OBGPermissionsInfo> list = permissionsInfoMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //查权限list
    public List<OBGPermissionsInfo> findByIdList(List<Integer> permissionIds) {
        OBGPermissionsInfoExample example = new OBGPermissionsInfoExample();
        OBGPermissionsInfoExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(permissionIds);
        List<OBGPermissionsInfo> permissionsInfos = permissionsInfoMapper.selectByExample(example);
        return permissionsInfos;
    }

    /**
     * 递归向上
     */
    public boolean checkParent(List<OBGPermissionsInfo> userPermList, Integer pid) {
        OBGPermissionsInfo parent = permissionsInfoMapper.selectByPrimaryKey(pid);
        if (parent == null) {
            return false;
        }
        if (userPermList.contains(parent)) {
            return true;
        } else {
            return checkParent(userPermList, parent.getParentId());
        }
    }
}
